<?php
namespace app\api\model\wanlshop;

use think\Model;

/**
 * 配置模型
 */
class ShopConfig extends Model
{

    // 表名,不含前缀
    protected $name = 'wanlshop_shop_config';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';


    public function getPayConfig($type,$method,$shop_id){
        if(!$shop_id){
            if($type == 'paylinx_alipay'){
                if($method == 'app'){
                    $ymypaylinxConfig = get_addon_config('ymypaylinx');
                    return $ymypaylinxConfig['mobile_alipay'];
                }elseif ($method == 'wap' ){
                    $ymypaylinxConfig = get_addon_config('ymypaylinx');
                    return $ymypaylinxConfig['mobile_alipay'];
                } elseif ($method == 'scan' ){
                    $ymypaylinxConfig = get_addon_config('ymypaylinx');
                    return $ymypaylinxConfig['pc_alipay'];
                }
                $ymypaylinxConfig = get_addon_config('ymypaylinx');
                return $ymypaylinxConfig['mobile_alipay'];
            }else{
                $ymypaylinxConfig = get_addon_config('ymypaylinx');
                return $ymypaylinxConfig['wechat'];
            }
        }else{
            $row = $this->get(['shop_id' => $shop_id]);
            $shop = model('app\api\model\wanlshop\Shop')->get($shop_id);
            if($shop['state'] == 2){
                return $this->getPayConfig($type,$method,0);
            }
            if($type == 'paylinx_alipay'){
                if($method == 'app'){
                    $ymypaylinxConfig = get_addon_config('ymypaylinx');
                    return $ymypaylinxConfig['mobile_alipay'];
                }elseif ($method == 'wap' ){
                    $config = json_decode($row['paylinx_mobile_alipay'],true);
                    if($config && $config['store_id'] && $config['mch_id'] && $config['key']){
                        return $config;
                    }

                }elseif ($method == 'scan' ){
                    $config = json_decode($row['paylinx_pc_alipay'],true);
                    if($config && $config['store_id'] && $config['mch_id'] && $config['key']){
                        return $config;
                    }

                }
                return $this->getPayConfig($type,$method,0);
            }else{
                if($method == 'app'){
                    $ymypaylinxConfig = get_addon_config('ymypaylinx');
                    return $ymypaylinxConfig['wechat'];
                }else{
                    $config = json_decode($row['paylinx_wechat'],true);
                    if($config && $config['store_id'] && $config['mch_id'] && $config['key']){
                        return $config;
                    }
                }
                return $this->getPayConfig($type,$method,0);
            }
        }
    }
}
